#include "StdAfx.h"
#include "VRecord.h"

// VRecord ////////////////////////////////////////////////////////////////////

//
void VRecord::clearAll() 
{ 
	qDeleteAll(m_fields);
	m_fields.clear();
}

//
void VRecord::clearValues() 
{ 
	for (int i = 0; i < fieldCount(); ++i) 
		field(i)->setValue(QVariant());
}

//
VField* VRecord::field(const QString& name)
{
	for (int i = 0; i < fieldCount(); ++i) 
	{
		VField *f = field(i);
		if (0 == f->name().compare(name,Qt::CaseInsensitive)) 
			return f;
	}
	return NULL;
}

//
bool VRecord::setField(int column,VField *field)
{
	if (column < 0 || column >= m_fields.count())
		return false;

	delete m_fields[column];
	m_fields[column] = field;
	return true;
}

//
int VRecord::indexOf(const QString& name)
{
	for (int i = 0; i < fieldCount(); i++)
	{
		VField *f = field(i);
		if (0 == f->name().compare(name,Qt::CaseInsensitive))
			return i;
	}
	return -1;
}

//
//void VRecord::addName(const QString& name) {
//	if (!containsName(name)) {
//		d->fields.append(VField(name));
//	}
//}

//
void VRecord::copyFrom(const VRecord& from_) 
{
	clearAll();
	VRecord& from = const_cast<VRecord&>(from_);

	for (int i = 0; i < from.fieldCount(); i++)
		appendField(	from.field(i)->clone() );
}

/**********
//
void VRecord::copyNamesFrom(const VRecord& from) {
	clear();
	for (int i = 0; i < from.d->fields.count(); i++) {
		d->fields.append(
			VField(from.d->fields[i].name())
		);
	}
}

//
void VRecord::setValuesFrom(const VRecord& from) {
	VField fromField;
	foreach (fromField,from.d->fields) {
		VField* thisField = findFieldPtrByName(fromField.name());
		if (thisField) {
			thisField->setValue(fromField.value());
		}
	}
}
****************/

//
void VRecord::setValue(const QString& name,const QVariant& value) 
{
	VField* f = field(name);
	if (f) 
		f->setValue(value);
}

//
QVariant VRecord::value(const QString& name)
{
	const VField* f = field(name);
	if (f) 
		return f->value();

	return QVariant();
}
